<template>
  <el-container class="homeContainer">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="" />
        <span>电商管理系统</span>
      </div>
      <el-button type="warning" @click="homeSignOut">退出</el-button>
    </el-header>
    <!-- 内容 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="iscollapse ? '64px' : '200px'">
        <div @click="menuCollapse">MENU</div>
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#ffffff"
          text-color="#000000"
          active-text-color="#ffd04b"
          :unique-opened="true"
          :collapse="iscollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="currentIndex"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + itemSon.path"
              v-for="itemSon in item.children"
              :key="itemSon.id"
              @click="activeNav('/' + itemSon.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ itemSon.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      iconsObj: {
        125: "iconfont icon-user",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      iscollapse: false,
      currentIndex: "",
    };
  },
  created() {
    this.getMenuList();
    this.currentIndex = window.sessionStorage.getItem("currentIndex");
  },
  methods: {
    // 退出登录,删除token
    homeSignOut() {
      window.sessionStorage.removeItem("token");
      this.$router.push("/");
    },
    // 获取渲染左侧菜单栏
    async getMenuList() {
      this.$http.get("menus").then((response) => {
        if (response.data.meta.status !== 200)
          return this.$message.error(response.data.meta.msg);
        this.menulist = response.data.data;
      });
    },
    // 菜单折叠展开
    menuCollapse() {
      this.iscollapse = !this.iscollapse;
    },
    // 二级菜单颜色
    activeNav(index) {
      this.currentIndex = index;
      //因为刷新页面点击后存储的data数据会消失,所以要加在绘画存储中一份,初始化的时候渲染上去
      window.sessionStorage.setItem("currentIndex", index);
    },
  },
  computed: {},
};
</script>

<style lang='less' scoped>
.homeContainer {
  height: 100%;
  .el-header {
    background-color: #ffffff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    div {
      display: flex;
      align-items: center;
      img {
        width: 40px;
        height: 40px;
      }
    }
  }
  .el-aside {
    background-color: #ffffff;
    > div {
      background-color: #ffffff;
      color: #000000;
      text-align: center;
      cursor: pointer;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
}
.iconfont {
  margin-right: 10px;
}
.el-menu-vertical-demo {
  border: none;
}
</style>>
